package com.whzykj.model;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.whzykj.common.CommonUtils;
import com.whzykj.common.DbUtil;
import com.whzykj.jfinal.TableBind;

@TableBind(name = "rechargebill", pk = "sn")
public class RechargeBill extends Model<RechargeBill> {
	private static final long serialVersionUID = 1L;
	public static final RechargeBill dao = new RechargeBill();
	
	/*****
	 * 后台预充值
	 * @param paraMap
	 */
	public boolean recharge(Map<String,Object> paraMap){
		
		String cust_id = CommonUtils.getMapKeyVal(paraMap, "cust_id");
		String userid = CommonUtils.getMapKeyVal(paraMap, "userid");
		String rechargeamount = CommonUtils.getMapKeyVal(paraMap, "amount");//充值金额
		String remarks=CommonUtils.getMapKeyVal(paraMap, "remarks");
		String acctsql=" SELECT * FROM account WHERE cust_id=? ";
		Record rd = Db.findFirst(acctsql,cust_id);
		int acct_id = 0;
		//判断账户是否存在
		if(rd == null)
		{
//			Account acct = new Account();
//			acct.set("cust_id", cust_id);
//			acct.set("amount", rechargeamount);
//			acct.set("account_type", "00");
//			acct.set("property", "01");
//			acct.set("state", "00");
//			acct.set("create_time", new Date());
//			acct.save();
//			acct_id=acct.getInt("acct_id");//账户ID
			return false;
			
		}else{
			acct_id = rd.getInt("acct_id");//账户ID
//			Account acct = Account.dao.findById(acct_id);
//			BigDecimal fixamount = new BigDecimal(rechargeamount);
//			acct.set("amount", fixamount.add(balanceamount));//账户资金变动后总金额
//			acct.update();
		}
		
		RechargeBill rebill = new RechargeBill();
		String sn = CommonUtils.generateRechargeBill();
		String recharge_type = CommonUtils.getMapKeyVal(paraMap, "recharge_type");
		String trade_no = CommonUtils.getMapKeyVal(paraMap, "trade_no");
		String buyer_login_id = CommonUtils.getMapKeyVal(paraMap, "buyer_login_id");
		String bank_type = CommonUtils.getMapKeyVal(paraMap, "bank_type");
		String bank_name = CommonUtils.getMapKeyVal(paraMap, "bank_name");
		
		rebill.set("sn", sn);
		rebill.set("acct_id", acct_id);
		rebill.set("recharge_type", recharge_type);
		rebill.set("recharge_person", "01");
		rebill.set("trade_no", trade_no);
		rebill.set("amount", rechargeamount);
		rebill.set("pay_time", new Date());
		rebill.set("buyer_login_id", buyer_login_id);
		rebill.set("bank_type", bank_type);
		rebill.set("bank_name", bank_name);
		rebill.set("trade_status", "00");
		rebill.set("create_time", new Date());
		rebill.set("recharge_state", "00");
		rebill.set("remarks", remarks);
		rebill.save();
		
//		AccountSeq acctseq = new AccountSeq();
//		acctseq.set("sn", CommonUtils.generateAcctSeq());
//		acctseq.set("acct_id", acct_id);
//		acctseq.set("seq_flag", "00");
//		acctseq.set("change_type", recharge_type);
//		acctseq.set("pre_amount", balanceamount);//账户资金变动前总金额
//		BigDecimal fixamount = new BigDecimal(rechargeamount);
//		acctseq.set("amount", fixamount.add(balanceamount));//账户资金变动后总金额
//		acctseq.set("ref_sn", sn);
//		acctseq.set("create_time", new Date());
//		acctseq.save();
		
		//判断活动金额是否存在
		String station_id = CommonUtils.getMapKeyVal(paraMap, "station_id");
		String sns ="";//生成活动充值记录
		int activity_id=-1;
		BigDecimal activity_amount=new BigDecimal("0.00");
		RechargeActivityDetails rechargedetails=RechargeActivityDetails.me.findValidRechargeActivityDetails(Integer.parseInt(station_id),Integer.parseInt(rechargeamount));
		if(rechargedetails != null)
		{
			activity_id =rechargedetails.getInt("activity_id");
			activity_amount = rechargedetails.getBigDecimal("activity_amount");
			sns = CommonUtils.generateRechargeBill();//生成活动充值记录
			
			RechargeBill rebills = new RechargeBill();
			rebills.set("sn", sns);
			rebills.set("acct_id", acct_id);
			rebills.set("recharge_type", "05");
			rebills.set("recharge_person", "01");
			rebills.set("amount", activity_amount);
			rebills.set("pay_time", new Date());
			rebills.set("trade_status", "00");
			rebills.set("create_time", new Date());
			rebills.set("recharge_state", "00");
			rebills.save();
		}
		
		RechargeActivityBill bill = new RechargeActivityBill();
		bill.set("acct_id", acct_id);
		bill.set("cust_id", cust_id);
		bill.set("activity_id", activity_id);
		bill.set("recharge_amount", rechargeamount);
		bill.set("recharge_sn", sn);
		bill.set("activity_amount", activity_amount);
		bill.set("activity_sn", sns);
		bill.set("create_time", new Date());
		bill.set("recharge_person", userid);
		boolean flag = bill.save();
		
		return flag;
	}
	
	/*****
	 * 后台确认充值
	 * @param paraMap
	 */
	public String confirmrecharge(Map<String,Object> paraMap){
		
        String sn = CommonUtils.getMapKeyVal(paraMap, "sn");
        String activity_sn = CommonUtils.getMapKeyVal(paraMap, "activity_sn");
		String cust_id = CommonUtils.getMapKeyVal(paraMap, "cust_id");
		String acct_id = CommonUtils.getMapKeyVal(paraMap, "acct_id");
		String userid = CommonUtils.getMapKeyVal(paraMap, "userid");
		
		Integer amount = Integer.parseInt(CommonUtils.getMapKeyVal(paraMap, "amount"));
		Integer activity_amount = Integer.parseInt(CommonUtils.getMapKeyVal(paraMap, "activity_amount"));
		
		
		List<String> sqlList=new ArrayList<String>();
		// 更新账户表中充值金额
		StringBuffer sql=new StringBuffer("update account set amount=amount+"+amount);
		sql.append(",last_update_time=now(),last_update_person="+userid+" WHERE acct_id="+acct_id);
		sqlList.add(sql.toString());
		
		// 添加资金变动表中的记录
		String zjsn=CommonUtils.generateAcctSeq();
		RechargeBill rb=RechargeBill.dao.findById(sn);
		String change_type=rb.getStr("recharge_type");
		Account acct=Account.dao.findById(acct_id);
		double preamount=acct.getBigDecimal("amount").doubleValue();
		sql=new StringBuffer("insert into account_seq(sn,acct_id,seq_flag,change_type,pre_amount,amount,ref_sn,create_time)");
		sql.append("values('"+zjsn+"',"+acct_id+",'00','"+change_type+"',"+preamount+","+(preamount+amount)+",'"+sn+"',now() )");
		sqlList.add(sql.toString());
		
		// 更新充值表中的记录
		String usql="update rechargebill set trade_status='02' where sn='"+sn+"'";
		sqlList.add(usql);
		
		// 如果充值存在活动赠送的情况，进行相应的处理
		if(activity_sn!=null && !activity_sn.trim().equals("")){
			if(activity_amount!=null && activity_amount.intValue()>0){
				sql=new StringBuffer("update account set amount=amount+"+activity_amount);
				sql.append(",last_update_time=now(),last_update_person="+userid+" WHERE acct_id="+acct_id);
				sqlList.add(sql.toString());
				
				String zasn=CommonUtils.generateAcctSeq();
				sql=new StringBuffer("insert into account_seq(sn,acct_id,seq_flag,change_type,pre_amount,amount,ref_sn,create_time)");
				sql.append("values('"+zasn+"',"+acct_id+",'00','05',"+(preamount+amount)+","+(preamount+amount+activity_amount)+",'"+activity_sn+"',now())");
				sqlList.add(sql.toString());
				
				usql="update rechargebill set trade_status='02' where sn='"+activity_sn+"'";
				sqlList.add(usql);
				
			}
		}
//		int[] cnts=Db.batch(sqlList, 100);
		boolean result=DbUtil.batch(sqlList);
		return "0";
	}
}
